import { getTypeList,getShopList } from "../../server/api.js";
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        allLIst:[],
        typeList:[],
        shopList:[],
        mainActiveIndex:0,
        cartLength:'',
        price:0,
        show:false,
        feiBox: "",
        hide_good_box: true,
    },
    // 获取分类列表
    async getTypeList(){
        const { data } = await getTypeList()
        if( data.status==0 ){
            data.data.forEach(v=>{
                v.text = v.type_name
            })
            
            this.setData({
                typeList:data.data
            })
        }
    },
    // 获取商品列表
    async getShopList(){
        const { data } = await getShopList()
        if( data.status==0 ){
            data.data.forEach(v=>{
                v.text = v.shop_name
                // 把v.info以、转换成数组
                v.infoList = v.info.split('、')
            })
            this.setData({
                shopList:data.data,
                allLIst:data.data
            })
        }
    },
    // 点击分类
    onClickNav(e) {
        this.setData({
          mainActiveIndex: e.detail.index || 0,
        });
        // 根据索引获取对应的分类id
        let id = this.data.typeList[e.detail.index].type_id
        // 根据分类id获取对应的商品
        if( id==9 ){
            this.setData({
                shopList:this.data.allLIst
            })
            return
        }else{
            let shopList = this.data.allLIst.filter(v=>v.type_id==id)
            this.setData({
                shopList:shopList
            })
        }
    },
    // 点击商品
    order(){
        wx.navigateTo({
          url: '../order/order',
        })
    },
    // 加入购物车
    addShop(e){
        this.dotMove(e)
        let item = e.currentTarget.dataset.item
        let shopCarList = app.globalData.shopCarList
        let index = shopCarList.findIndex(v=>v.shop_id==item.shop_id)
        if(index==-1){
            item.num = 1
            shopCarList.push(item)
        }else{
            shopCarList[index].num++
        }
        app.globalData.shopCarList = shopCarList
        // 计算商品数量
        let total = 0
        shopCarList.forEach(v=>{
            total += v.num
        })
        // 计算总价
        let price = 0
        shopCarList.forEach(v=>{
            price += v.num*v.price
        })
        // 精确到两位小数
        price = price.toFixed(2)
        this.setData({cartLength:total,price:price})
    },
    // 关闭弹窗
    close(){
        this.setData({show:false})
    },
    // 购物车动画-三次贝塞尔曲线
    dotMove(e){
        //把点击每一项的对应的商品图保存下来，就是飞向购物车的图片
        this.setData({
            feiBox: e.currentTarget.dataset.item.shop_img
        })
        // 如果good_box正在运动
        if (!this.data.hide_good_box) return;
        //当前点击位置的x，y坐标
        this.finger = {};
        var topPoint = {};
        this.finger['x'] = e.touches["0"].clientX;
        this.finger['y'] = e.touches["0"].clientY - 20;
        if (this.finger['y'] < this.busPos['y']) {
            topPoint['y'] = this.finger['y'] - 150;
        } else {
            topPoint['y'] = this.busPos['y'] - 150;
        }

        if (this.finger['x'] < this.busPos['x']) {
            topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2 + this.finger['x'];
        } else {
            topPoint['x'] = this.busPos['x'];
            this.finger['x'] = this.busPos['x']
        }


        this.linePos = app.bezier([this.finger, topPoint, this.busPos], 30);
        this.startAnimation();
    },
    //开始动画
    startAnimation: function() {
        var index = 0,
            that = this,
            bezier_points = that.linePos['bezier_points'];
        this.setData({
            hide_good_box: false,
            bus_x: that.finger['x'],
            bus_y: that.finger['y']
        })
        this.timer = setInterval(function() {
            index++;
            that.setData({
                bus_x: bezier_points[index]['x'],
                bus_y: bezier_points[index]['y']
            })
            if (index >= 28) {
                clearInterval(that.timer);
                that.setData({
                    hide_good_box: true,
                    hideCount: false,
                    count: that.data.count += 1
                })
            }
        }, 33);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({show:true})
        this.getTypeList()
        this.getShopList()
        var that = this;
        //可视窗口x,y坐标
        this.busPos = {};
        this.busPos['x'] = app.globalData.ww * .85;
        this.busPos['y'] = app.globalData.hh * .85;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.getUserProfile()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        if (this._observer) this._observer.disconnect()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})